package com.example.easytts.controller

import com.example.easytts.model.Options
import com.example.easytts.service.OptionsService
import com.example.easytts.repository.LanguageRepository
import com.example.easytts.repository.VoiceRepository
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.*

@RestController
@RequestMapping("/api/options")
class OptionsController(
    private val optionsService: OptionsService,
    private val languageRepository: LanguageRepository,
    private val voiceRepository: VoiceRepository
) {
    @GetMapping
    fun getOptions(): Options {
        return optionsService.getOptions()
    }

    @PostMapping("/max-chars-per-day")
    fun setMaxCharsPerDay(@RequestParam value: Int): ResponseEntity<Any> {
        optionsService.setMaxCharsPerDay(value)
        return ResponseEntity.ok(mapOf("message" to "设置成功", "maxCharsPerDay" to value))
    }

    @GetMapping("/voice-options")
    fun getVoiceOptions(): Map<String, List<Map<String, Any?>>> {
        val langs = languageRepository.findAll()
        val voices = voiceRepository.findAll()
        val result = mutableMapOf<String, List<Map<String, Any?>>>()
        langs.forEach { lang ->
            val langVoices = voices.filter { it.languageId == lang.uuid }
            result[lang.name] = langVoices.map { v ->
                mapOf(
                    "uuid" to v.uuid,
                    "shortName" to v.shortName,
                    "localName" to v.localName,
                    "avatar" to v.avatar,
                    "gender" to v.gender,
                    "language" to v.languageName,
                    "styleCount" to v.styleCount,
                    "desc" to v.description,
                    "audioUrl" to v.audioUrl,
                    "ageGroups" to v.ageGroups,
                    "locale" to v.locale,
                    "styleSamples" to v.styleSamples
                )
            }
        }
        return result
    }
} 